package com.xh.sys.controller;

import com.xh.common.R;
import com.xh.sys.entity.SysCommentBackItm;
import com.xh.sys.service.SysCommentBackItmService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 浏览评论回复功能_详细内容 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/sys-comment-back-itm")
@RequiredArgsConstructor
@Validated
public class SysCommentBackItmController {
    private final SysCommentBackItmService sysCommentBackItmService;

    /**
     * 添加/修改 评论
     * 必须送上主题id
     *
     * @param tosave
     * @return
     */
    @PostMapping("/save")
    public R<SysCommentBackItm> save(@RequestBody
                                     @Validated
                                             SysCommentBackItm tosave) {
        boolean saveFlag = sysCommentBackItmService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(sysCommentBackItmService.getById(tosave.getId()));
        }
        return R.failed();
    }


    /**
     * 通过主题id查全部评论
     *
     * @param commentBackId
     * @return
     */
    @PostMapping("/queryByCommentBackId/{commentBackId}")
    public R<List<SysCommentBackItm>> queryByCommentBackId(@PathVariable
                                                           @Validated
                                                           @NotNull(message = "id 不能空")
                                                                   Integer commentBackId) {

        List<SysCommentBackItm> list1 = sysCommentBackItmService.queryByCommentBackId(commentBackId);

        return R.success(list1);
    }


    /**
     * 删除评论 支持多个  上送要删除的节点 及子节点
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<SysCommentBackItm> del(@RequestBody
                                    @Validated
                                    @NotEmpty(message = "ids 集合不能空")
                                            List<Integer> ids) {
        //sysCommentBackItmService.del(ids);
        sysCommentBackItmService.removeByIds(ids);
        return R.success("删除成功");
    }

}
